#include <bits/stdc++.h>
using namespace std;
const int N = 100020;
int n, m;
int par[N];
int arr[N];  // 这题不是最小生成树，而是将需要交换的奶牛都连起来
struct edge {
  int a, b, w;
  bool operator<(const edge& x) const { return w > x.w; }
} h[N];
int find(int x) { return par[x] = x == par[x] ? x : find(par[x]); }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) par[i] = i;
  for (int i = 1; i <= n; ++i) cin >> arr[i];
  for (int i = 1; i <= m; ++i) cin >> h[i].a >> h[i].b >> h[i].w;
  int ans = -1, cnt = 0;
  sort(h + 1, h + m + 1);
  for (int i = 1; i <= m; ++i) {
    // 判断是否连通
    while (find(cnt) == find(arr[cnt])) {
      cnt++;
      if (cnt >= n) {
        goto End;
      }
    }
    auto [a, b, w] = h[i];
    int fa = find(a), fb = find(b);
    if (fa != fb) {
      par[fa] = fb;
      ans = w;
    }
  }
End:
  cout << ans << endl;
  return 0;
}
